Load required libraries

library(readxl)
library(ggplot2)
library(RColorBrewer)   
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(webshot)

Read xls into a dataframe

obama_approvals_df <- read_excel(path = "C:/Masters/GitHub/Summer2023/DSC640-Data Presentation & Visualization/Week1&2/Data/obama-approval-ratings.xls")
nrow(obama_approvals_df)
## [1] 13
head(obama_approvals_df,5)
## # A tibble: 5 × 4
##   Issue           Approve Disapprove  None
##   <chr>             <dbl>      <dbl> <dbl>
## 1 Race Relations       52         38    10
## 2 Education            49         40    11
## 3 Terrorism            48         45     7
## 4 Energy Policy        47         42    11
## 5 Foreign Affairs      44         48     8

DONUT CHART

#fig <- obama_approvals_df %>% plot_ly(labels = ~Issue, values = ~Approve, textposition = 'outside', #textinfo='percent+label', width = 1)
#fig <- fig %>% add_pie(hole = 0.6)
#fig <- fig %>% layout(title = list(text='Donut charts using Plotly'), 
#                      autosize = T ,showlegend = F)
#fig


fig1 <- plot_ly(obama_approvals_df, labels = ~Issue, values = ~Approve, #type = 'pie',
        textposition = 'inside',
        textinfo = 'percent+label',
        text = ~paste(Issue), 
        showlegend = FALSE)
fig1 <- fig1 %>% add_pie(hole = 0.6)
fig1 <- fig1 %>% layout(title = 'Approval Rates by Issue',autosize = T)
fig1

PIE CHART

#Pie Chart
fig <- obama_approvals_df %>% plot_ly(labels = ~Issue, values = ~Disapprove,
                                      type = 'pie', textposition = 'inside', 
                                      textinfo='value+label')
fig <- fig %>% layout(title = "Disapproval Counts per Issue",autosize = T,  showlegend = F) 
fig

STACKED BAR CHART

#Stacked Bar Chart
fig <- plot_ly(obama_approvals_df)
fig <- fig %>% add_trace(x = ~Issue,y = ~None, name = 'None',text = ~None, type='bar',
                         marker = list(color = 'rgb(127, 200, 250)',
                                       line = list(color = 'rgb(8,48,107)' )))
fig <- fig %>% add_trace(x = ~Issue,y = ~Disapprove, name = 'Disapprove',text = ~Disapprove, type='bar',
                         marker = list(color = 'rgb(45, 167, 250)',
                                       line = list(color = 'rgb(8,48,107)')))
fig <- fig %>% add_trace(x = ~Issue,y = ~Approve, name = 'Approve', text = ~Approve, type='bar',
                         marker = list(color = 'rgb(4, 74, 122)',
                                       line = list(color = 'rgb(8,48,107)')))
fig <- fig %>% layout(title = "Approval, Disapproval and Not-Accounted(None) counts by Issue",yaxis = list(title = 'Count'),autosize = T, 
                      barmode = 'stack',xaxis = list(title = "Issue", tickangle = -75))
fig

BAR CHART

#Bar Chart
fig <- plot_ly(obama_approvals_df,x = ~Issue,y = ~Disapprove, name = 'Disapprove',
               text = ~Disapprove, type='bar',
               marker = list(color = 'rgb(20, 71, 252)',
                             line = list(color = 'rgb(8,48,107)'))) 
fig <- fig %>% layout(title="Disapproval counts per Issue",autosize = T,yaxis = list(title = 'Count'),
                      xaxis = list(title = "Issue", tickangle = -75))
fig

LINE CHART

# Line Chart

fig <- plot_ly(obama_approvals_df, x = ~Issue) 
fig <- fig %>% add_lines(y = ~Disapprove, name = "Disapprove")
fig <- fig %>% add_lines(y = ~Approve, name = "Approve") 
fig <- fig %>% layout(title="Approvals and Disapproval by Issue",autosize = T)
fig